package com.bifrost.config;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author rangf
 * @date 2018/2/24
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="sellEntityManagerFactory",
        transactionManagerRef="transactionManagerTertiary",
        basePackages= { "com.bifrost.sellRepository" })
public class SellRepositoryConfig {

    @Autowired
    private JpaProperties jpaProperties;
    @Autowired
    @Qualifier("sellDataSource")
    private DataSource sellDataSource;

    @Bean(name = "sellEntityManager")
    public EntityManager cbEntityManager(EntityManagerFactoryBuilder builder) {
        return cbEntityManagerFactory(builder).getObject().createEntityManager();
    }

    @Bean(name = "sellEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean cbEntityManagerFactory (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(sellDataSource)
                .properties(getVendorProperties(sellDataSource))
                .packages("com.bifrost.sellentity")
                .persistenceUnit("sellPersistenceUnit")
                .build();
    }

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        jpaProperties.setDatabase(Database.ORACLE);
        Map<String,String> map = new HashMap<>(2);
        map.put("hibernate.hbm2ddl.auto","none");
        map.put("hibernate.dialect","org.hibernate.dialect.Oracle10gDialect");
        jpaProperties.setProperties(map);
        return  jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "transactionManagerTertiary")
    PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(cbEntityManagerFactory(builder).getObject());
    }
}
